<?php
class admin_listController extends adminController{

    //首页
    public function index() {
        $this->app_list=model('app_manage')->app_list();
        $this->show();  
    }

    //安装
    public function install() {
        $name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if($info['APP_INSTALL']==1){
        	$this->msg('该APP已经安装过了！',0);
        }
        if($info['APP_RELY']){
        	$rely=explode(',', $info['APP_RELY']);
        	if(!empty($rely)){
        		foreach ($rely as $value) {
                    $app_config=appConfig($value);
                    if(empty($app_config)){
                        $this->msg('没有发现依赖APP：'.$value,0);
                    }
                    if(!$app_config['APP_STATE']){
                        $this->msg('请先开启'.$app_config['APP_NAME'].'后才能操作本APP！',0);
                    }
        			
        		}
        	}
        }
        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'install_data/';
        if(is_dir($db_dir)){
            //获取站点列表
            $site_list=api('site_manage','site_list');
            foreach ($site_list as $value) {
                $site_info=api('site_manage','site_info',$value['SITE_MARK']);
                //导入数据库
                $db = @new Dbbak($site_info['DB_HOST'].':'.$site_info['DB_PORT'],$site_info['DB_USER'],$site_info['DB_PWD'],$site_info['DB_NAME'],'utf8',$db_dir);
                if($db){
                    @$db->importSql('',$info['APP_ORIGINAL_PREFIX'],$site_info['DB_PREFIX']);
                }
            }
        }

        //写入配置
        $data=array();
        $data['APP_STATE']=1;
        $data['APP_INSTALL']=1;
		if( save_config($name, $data) ){
            cacheApi();
            api($name,'app_install');
			$this->msg('安装成功！');
		}	
		$this->msg('安装失败！可能由于您的目录没有读写权限！',0);
    }

    //卸载
    public function uninstall() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
        	$this->msg('该APP已经卸载或者未安装！',0);
        }
        if($info['APP_STATE']){
            $this->msg('请先禁用本APP才能进行卸载！',0);
        }
        //卸载数据库
        if(!empty($info['APP_TABLES'])){
        	model('app_manage')->uninstall($info['APP_TABLES']);
        }
        api($name,'app_uninstall');
        //写入配置
        $data=array();
        $data['APP_STATE']=0;
        $data['APP_INSTALL']=0;
        if( save_config($name, $data) ){
            cacheApi();
            $this->msg('卸载成功，请手动FTP删除文件！');
        }
    }


    //备份
    public function export() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
        	$this->msg('该APP已经卸载或者未安装！',0);
        }
        //导出数据库
        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'data/'.DEFAULT_SITE.'/';
        if(!empty($info['APP_TABLES'])){
        	$config=config('DB');
        	$db = new Dbbak($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],'utf8',$db_dir);
        	$tables = explode(',', $info['APP_TABLES']);
        	foreach ($tables as $table) {
        		$table_array[]=$config['DB_PREFIX'].$table;
        	}
	        if(!$db->exportSql($table_array)){
	            $this->msg('数据备份失败！',0);
	        }
	    }
	    //移动文件
        $public_dir=ROOT_PATH . 'public/';
        if(is_dir($public_dir)){
        	copy_dir($public_dir . $name , $app_base.'public/' );
        }
        api($name,'app_backup');
		$this->msg('备份当前数据成功！');
    }

    //恢复
    public function restore() {
        $name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
            $this->msg('该APP已经卸载或者未安装！',0);
        }

        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'data/'.DEFAULT_SITE.'/';
        if(is_dir($db_dir)){
            //导入数据库
            $config=config('DB');
            $db = new Dbbak($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],'utf8',$db_dir);
            $db->importSql('',$info['APP_ORIGINAL_PREFIX'],$config['DB_PREFIX']);
        }else{
            $this->msg('没有发现备份过的数据！');
        }
        api($name,'app_restore');
        $this->msg('当前APP数据恢复成功！',0);
    }

    //打包
    public function package() {
        $name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        if(!$info['APP_INSTALL']){
            $this->msg('该APP已经卸载或者未安装！',0);
        }
        //导出数据库
        $app_base=BASE_PATH . 'apps/' . $name . '/';
        $db_dir = $app_base.'install_data/';
        $config=config('DB');
        if(!empty($info['APP_TABLES'])){
            $db = new Dbbak($config['DB_HOST'],$config['DB_USER'],$config['DB_PWD'],$config['DB_NAME'],'utf8',$db_dir);
            $tables = explode(',', $info['APP_TABLES']);
            foreach ($tables as $table) {
                $table_array[]=$config['DB_PREFIX'].$table;
            }
            if(!$db->exportSql($table_array)){
                $this->msg('数据打包失败！',0);
            }
        }
        //移动文件
        $public_dir=ROOT_PATH . 'public/';
        if(is_dir($public_dir)){
            copy_dir($public_dir . $name , $app_base.'public/' );
        }
        api($name,'app_package');
        $data=array();
        $data['APP_ORIGINAL_PREFIX']=$config['DB_PREFIX'];
        save_config($name, $data);        
        $this->msg('当前APP数据打包安装安装成功！');
    }

    //改变状态
    public function status() {
    	$name=in($_POST['name']);
        $this->alert_str($name,'',true);
        //获取APP信息
        $info=appConfig($name);
        //设置状态
        if(!$info['APP_STATE']){
            //获取当前APP依赖
            $app_rely=$info['APP_RELY'];
            if(!empty($app_rely)){
                $app_rely_list=explode(',', $app_rely);
                if(!empty($app_rely_list)){
                    foreach ($app_rely_list as $value) {
                        $app_config=appConfig($value);
                        if(!$app_config['APP_STATE']){
                            $this->msg('请先开启'.$app_config['APP_NAME'].'后才能操作本APP！',0);
                        }
                    }
                }
            }
        	$status=1;
        }else{
            //检测是否存在下级依赖
            $app_list=getApps();
            foreach ($app_list as $value) {
                $app_config=appConfig($value);
                $app_rely=$app_config['APP_RELY'];
                if(empty($app_rely)){
                    continue;
                }
                $app_rely_list=explode(',', $app_rely);
                if(in_array($name, $app_rely_list)){
                    if($app_config['APP_STATE']){
                        $this->msg('请先关闭'.$app_config['APP_NAME'].'后才能操作本APP！',0);
                    }
                }
            }
        	$status=0;
        }
        //写入配置
        $data=array();
        $data['APP_STATE']=$status;
		if( save_config($name, $data) ){
			$this->msg('状态更改成功！');
		}
		$this->msg('状态更改失败！可能由于您的目录没有读写权限！',0);

    }




}